//
//  LZADViewController.m
//  LZHundredJie
//
//  Created by apple on 16/4/1.
//  Copyright © 2016年 m14a.cn. All rights reserved.
//

#import "LZADViewController.h"
#import "LZTabBarController.h"
#import "UIImageView+WebCache.h"
#import <MJExtension/MJExtension.h>
#import <AFNetworking/AFNetworking.h>
#import "LZADitem.h"


#define LZCode2 @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"

@interface LZADViewController ()

/** 启动图片*/
@property (weak, nonatomic) IBOutlet UIImageView *lauchImageView;

@property (weak, nonatomic) IBOutlet UIView *adView;
/** 跳过按钮*/
@property (weak, nonatomic) IBOutlet UIButton *jumpBtn;
/** UIImageView对象*/
@property (nonatomic, weak) UIImageView *imageView;
/** 定时器*/
@property (nonatomic, weak) NSTimer *timer;
/** 模型数据*/
@property (nonatomic, strong) LZADItem *item;

@end

@implementation LZADViewController

#pragma mark - UIImageView懒加载
- (UIImageView *)imageView
{
    if (_imageView == nil) {
        // 1.创建对象
        UIImageView *imageView = [[UIImageView alloc] init];
        // 2.赋值给本地
        _imageView = imageView;
        // 3.添加到本地的广告View上
        [self.adView addSubview:_imageView];
        // 4.设置可以和用户点击
        imageView.userInteractionEnabled = YES;
        // 5.添加手势
        // 5.1创建手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
        // 5.2添加
        [imageView addGestureRecognizer:tap];
    }
    return _imageView;
}

/*
 业务逻辑:
 1.点击广告图片 跳转到广告界面 用safari打开
 2.广告界面倒计时
 3.点击跳过,直接进入到主框架界面
 */
// 点击手势过来这里
#pragma mark - 点击广告图片
- (void)tap
{
    UIApplication *app = [UIApplication sharedApplication];
    
    if ([app canOpenURL:[NSURL URLWithString:_item.ori_curl]]) { // 判断是否可以打开
        [app openURL:[NSURL URLWithString:_item.ori_curl]];
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    // 设置启动图片 -> 根据不同屏幕 加载不同图片 (屏幕适配)
    [self setupLaunchImage];

    // 加载广告数据
    [self loadData];
    
    // 创建定时器
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(textChange) userInfo:nil repeats:YES];
}

#pragma mark -定时器触发的方法
- (void)textChange
{
    static int i = 3;
    if (i <= 0) {
        [self clickJump];
    }
    i--;
    // 设置按钮标题
    [self.jumpBtn setTitle:[NSString stringWithFormat:@"跳过 (%zd)", i] forState:UIControlStateNormal];
}

// 跳过
- (IBAction)clickJump
{
    // 跳转界面
    LZTabBarController *tabBarVC = [[LZTabBarController alloc] init];
    [UIApplication sharedApplication].keyWindow.rootViewController = tabBarVC;
    // 销毁定时器
    [self.timer invalidate];
}

// 解析数据==写成plist文件==字典转模型==模型数据展示text/html
// ori_curl,w_picurl,w,h
- (void)loadData
{
    // 1.创建可变字典
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"code2"] = LZCode2;
    // 2.发送请求
    [LZHttpTool get:@"http://mobads.baidu.com/cpro/ui/mads.php" params:params success:^(id responseObj) {
        // 拿到字典
        NSDictionary *adDict = [responseObj[@"ad"] firstObject];
        // 字典转模型
        _item = [LZADItem mj_objectWithKeyValues:adDict];
        // 展示界面
        CGFloat w = ScreenW;
        CGFloat h = ScreenW * _item.h / _item.w;
        // 设置尺寸
        self.imageView.frame = CGRectMake(0, 0, w, h);
        // 加载广告图片,如果有数据就显示,没有的话，就不显示呗
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:_item.w_picurl]];
        
    } failure:^(NSError *error) {
        
    }];
    
}

- (void)setupLaunchImage
{
    UIImage *image = nil;
    if (iphone6P) {
        image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
    }else if (iphone6) {
        image = [UIImage imageNamed:@"LaunchImage-800-667h"];
    } else if (iphone5) {
        image = [UIImage imageNamed:@"LaunchImage-700-568h"];
    } else if (iphone4) {
        image = [UIImage imageNamed:@"LaunchImage-700"];
    }
    
    self.lauchImageView.image = image;
}



@end
